package site.marqstree.kotlin.library.good.ui.fragment.category.list.recyclerview

import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import org.jetbrains.anko.textColor
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.ui.fragment.category.list.bean.CategoryBean
import site.marqstree.kotlin.library.good.ui.fragment.category.list.listener.ICategoryList


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.fragment.category.list
 * 文件名: ListAdapter
 * 创建者: marqstree
 * 创建时间: 2020/3/6 16:06
 * 描述: TODO
 */
class CategoryListAdapter(private var mList: MutableList<CategoryBean> = mutableListOf<CategoryBean>(),
                            private val mListener:ICategoryList):
        BaseQuickAdapter<CategoryBean,BaseViewHolder>(R.layout.item_category_list,mList),
    OnItemClickListener {

    private var mCurrentPosition:Int = 0

    init {
        mListener.changeCategoryId(getCurrentCategoryId())
        setOnItemClickListener(this)
    }

    override fun convert(helper: BaseViewHolder, item: CategoryBean) {
        val tvName = helper.getView<TextView>(R.id.tv_vertical_item_name)
        tvName.text = item?.name

        if(item.isSelected){
            tvName.run{
                textColor = ContextCompat.getColor(context,
                    R.color.common_blue_light)
                background = ContextCompat.getDrawable(context,
                    R.drawable.border_left_blue)
                //设置为加粗
                setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }else{
            tvName.run{
                textColor = ContextCompat.getColor(context,
                    R.color.text_light_dark)
                background = ContextCompat.getDrawable(context,
                    R.drawable.border_bottom_gray)
                //设置为加粗
                setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }
        }
    }

    fun getCurrentCategoryId():Int{
        return mList.get(mCurrentPosition).id
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if (position != mCurrentPosition) {
            //还原上一个
            data.get(mCurrentPosition).isSelected=false
            notifyItemChanged(mCurrentPosition)

            //更新选中的item
            data.get(position).isSelected=true
            notifyItemChanged(position)
            mCurrentPosition = position

            mListener.changeCategoryId(getCurrentCategoryId())
        }
    }
}